package com.study.www.config;

import com.sun.org.apache.regexp.internal.RE;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 错误界面管理
 *
 * @author : Administrator.zhuyanpeng
 * @date : 2019/11/13 - 9:35
 */
@Configuration
public class ErrorPageManage  implements WebMvcConfigurer {
    public enum UrlPath {
        // 404 错误路径
        NOT_FOUND("/error/404_error.html"),
        INTERNAL_SERVER_ERROR("/error/500_error.html"),
        ;
        private String url;

        UrlPath(String url) {
            this.url = url;
        }

    }
    /**
     * 错误界面指定
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return (factory -> {
            //404界面
            factory.addErrorPages(
                    // 404错误
                    new ErrorPage(HttpStatus.NOT_FOUND, UrlPath.NOT_FOUND.url),
                    // 500错误
                    new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, UrlPath.INTERNAL_SERVER_ERROR.url)
            );
        });
    }

    /**
     * springMvc 视图控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(UrlPath.NOT_FOUND.url).setViewName(UrlPath.NOT_FOUND.url);
        registry.addViewController(UrlPath.INTERNAL_SERVER_ERROR.url).setViewName(UrlPath.INTERNAL_SERVER_ERROR.url);
    }
}
